package GUI;

import java.awt.Dimension;
import java.awt.Image;

import java.io.File;
import java.net.MalformedURLException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.gstreamer.Gst;

import com.mysql.jdbc.exceptions.*;

import database.Reader;
import database.Update;
import database.Writer;
import sponsor.*;
import rss.*;
import utils.ExtractVideo;
import video.Metadata;
import video.Player;
import video.VideoOrganizer;

/**
 * @author Cassio Soares Cabral
 */
@SuppressWarnings("serial")
public class MainScreen extends javax.swing.JFrame {

	private List<String> sponsorsNameList;
	private List<String> sponsorsTypeList;
	private List<Double> sponsorsMonthlyList;
	private List<Sponsor> sponsorsList;
	private HashMap<String, News> newsMap;
	private Writer writer;
	private Reader reader;
	private Update updater;
	private News news;
	private List<News> newsList;
	private VideoOrganizer videoOrganizer;
	private String video;

	/**
	 * Creates new form mainScreen
	 * 
	 * @throws Exception
	 * 
	 * @throws MySQLIntegrityConstraintViolationException
	 */
	public MainScreen() {

		writer = new Writer();
		reader = new Reader();
		updater = new Update();
		newsList = new ArrayList<News>();
		sponsorsNameList = new ArrayList<String>();
		sponsorsTypeList = new ArrayList<String>();
		sponsorsMonthlyList = new ArrayList<Double>();
		sponsorsList = new ArrayList<Sponsor>();
		newsMap = new HashMap<String, News>();

		// Recovery information about sponsors from database and put it in a
		// list
		try {
			for (int i = 0; i < reader.getSponsorsName().size(); i++) {
				Sponsor sponsorTemp = new Sponsor(reader.getSponsorsName().get(
						i), reader.getSponsorType().get(i), reader
						.getSponsorMonthly().get(i));
				sponsorsList.add(sponsorTemp);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(getRootPane(), e.getMessage(),
					"Erro!", JOptionPane.ERROR_MESSAGE);
		}
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	/**
	 * 
	 */
	private void initComponents() {

		Image im = null;
		try {
			im = new ImageIcon(Class.class.getResource("/images/logotipo.png"))
					.getImage();
			ImageIconLogotype = new ImageIcon(im);
			JLabelLogotype = new JLabel(ImageIconLogotype);
		} catch (NullPointerException ex) {
			ImageIconLogotype = new ImageIcon("images/logotipo.png");
			JLabelLogotype = new JLabel(ImageIconLogotype);
		}
		dialogAddVideo = new javax.swing.JDialog();
		jFileChooserAddVideo = new javax.swing.JFileChooser();
		jMenuBar1 = new javax.swing.JMenuBar();
		jMenuStart = new javax.swing.JMenu();
		jMenuItemStartExibition = new javax.swing.JMenuItem();
		jMenuItemExit = new javax.swing.JMenuItem();
		jMenuVideo = new javax.swing.JMenu();
		jMenuItemAddVideo = new javax.swing.JMenuItem();
		jMenuItemRemoveVideo = new javax.swing.JMenuItem();
		JMenuNews = new javax.swing.JMenu();
		jMenuItemUpdateNews = new javax.swing.JMenuItem();
		jMenuItemAddRss = new javax.swing.JMenuItem();
		jMenuItemRemoveRss = new javax.swing.JMenuItem();
		jMenuItemShowAvailable = new javax.swing.JMenuItem();
		jMenuManager = new javax.swing.JMenu();
		jMenuItemAddSponsor = new javax.swing.JMenuItem();
		jMenuItemChangeLevelSponsor = new javax.swing.JMenuItem();
		jMenuItemRemoveSponsor = new javax.swing.JMenuItem();
		jMenuItemSponsorAvailable = new javax.swing.JMenuItem();
		jMenuHelp = new javax.swing.JMenu();
		JMenuItemAbout = new javax.swing.JMenuItem();

		jFileChooserAddVideo
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jFileChooserAddVideoActionPerformed(evt);
					}
				});

		javax.swing.GroupLayout dialogAddVideoLayout = new javax.swing.GroupLayout(
				dialogAddVideo.getContentPane());
		dialogAddVideo.getContentPane().setLayout(dialogAddVideoLayout);
		dialogAddVideoLayout
				.setHorizontalGroup(dialogAddVideoLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								dialogAddVideoLayout
										.createSequentialGroup()
										.addComponent(
												jFileChooserAddVideo,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												592, Short.MAX_VALUE)
										.addContainerGap()));
		dialogAddVideoLayout
				.setVerticalGroup(dialogAddVideoLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								dialogAddVideoLayout
										.createSequentialGroup()
										.addComponent(
												jFileChooserAddVideo,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												408, Short.MAX_VALUE)
										.addContainerGap()));

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setSize(new Dimension(640, 480));

		jMenuStart.setText("Iniciar");

		jMenuItemStartExibition.setText("Iniciar Exibição");
		jMenuItemStartExibition
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						try {
							jMenuItemStartExibitionActionPerformed(evt);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
		jMenuStart.add(jMenuItemStartExibition);

		jMenuItemExit.setText("Sair");
		jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItemExitActionPerformed(evt);
			}
		});
		jMenuStart.add(jMenuItemExit);

		jMenuBar1.add(jMenuStart);

		jMenuVideo.setText("Vídeo");

		jMenuItemAddVideo.setText("Adicionar vídeo");
		jMenuItemAddVideo
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemAddVideoActionPerformed(evt);
					}
				});
		jMenuVideo.add(jMenuItemAddVideo);

		jMenuItemRemoveVideo.setText("Remover vídeo");
		jMenuItemRemoveVideo
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemRemoveVideoActionPerformed(evt);
					}
				});
		jMenuVideo.add(jMenuItemRemoveVideo);

		jMenuBar1.add(jMenuVideo);

		JMenuNews.setText("Notícia");

		jMenuItemUpdateNews.setText("Atualizar Notícias");
		jMenuItemUpdateNews
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						try {
							jMenuItemUpdateNewsActionPerformed(evt);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
		JMenuNews.add(jMenuItemUpdateNews);

		jMenuItemAddRss.setText("Adicionar RSS");
		jMenuItemAddRss.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jMenuItemAddRssActionPerformed(evt);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		JMenuNews.add(jMenuItemAddRss);

		jMenuItemRemoveRss.setText("Remover RSS");
		jMenuItemRemoveRss
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemRemoveRssActionPerformed(evt);
					}
				});
		JMenuNews.add(jMenuItemRemoveRss);

		jMenuItemShowAvailable.setText("Exibir RSS disponíveis");
		jMenuItemShowAvailable
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemShowAvailableActionPerformed(evt);
					}
				});
		JMenuNews.add(jMenuItemShowAvailable);

		jMenuBar1.add(JMenuNews);

		jMenuManager.setText("Administrar");

		jMenuItemAddSponsor.setText("Adicionar Patrocinador");
		jMenuItemAddSponsor
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemAddSponsorActionPerformed(evt);
					}
				});

		jMenuManager.add(jMenuItemAddSponsor);

		jMenuItemRemoveSponsor.setText("Remover patrocinador");
		jMenuItemRemoveSponsor
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemRemoveSponsorActionPerformed(evt);
					}
				});
		jMenuManager.add(jMenuItemRemoveSponsor);

		jMenuItemSponsorAvailable.setText("Patrocinadores disponíveis");
		jMenuItemSponsorAvailable
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemSponsorAvailableActionPerformed(evt);
					}
				});
		jMenuManager.add(jMenuItemSponsorAvailable);

		jMenuItemChangeLevelSponsor.setText("Mudar tipo de Patrocinador");
		jMenuItemChangeLevelSponsor
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jMenuItemChangeLevelSponsorActionPerformed(evt);
					}
				});
		jMenuManager.add(jMenuItemChangeLevelSponsor);

		jMenuBar1.add(jMenuManager);

		jMenuHelp.setText("Ajuda");

		JMenuItemAbout.setText("sobre");
		JMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				JMenuItemAboutActionPerformed(evt);
			}
		});
		jMenuHelp.add(JMenuItemAbout);

		jMenuBar1.add(jMenuHelp);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 393,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 241,
				Short.MAX_VALUE));
		// ************************************************************************************************
		JLabelLogotype.setSize(this.getWidth(), this.getHeight());
		add(JLabelLogotype);
		// ************************************************************************************************
	}// </editor-fold>

	private void jMenuItemAddVideoActionPerformed(java.awt.event.ActionEvent evt) {
		int preferedHeight = 450, preferedWidth = 620;
		dialogAddVideo.setVisible(true);
		dialogAddVideo.setSize(preferedWidth, preferedHeight);
	}

	private void jFileChooserAddVideoActionPerformed(
			java.awt.event.ActionEvent evt) {
		if (evt.getActionCommand() == jFileChooserAddVideo.APPROVE_SELECTION) {

			video = jFileChooserAddVideo.getSelectedFile().getPath();

			try {

				String[] options = new String[sponsorsList.size()];
				for (int i = 0; i < sponsorsList.size(); i++) {
					options[i] = sponsorsList.get(i).getName();
				}

				String nameOfSponsor = (String) JOptionPane.showInputDialog(
						getRootPane(),
						"Selecione o patrocinador a que pertence o video",
						"Adicionar Video", JOptionPane.PLAIN_MESSAGE, null,
						options, options[0]);

				List<String> pathsSponsor = reader
						.getPathOwnedBySponsor(nameOfSponsor);
				if (!(pathsSponsor.contains(video))) {
					for (int i = 0; i < sponsorsList.size(); i++) {
						if (sponsorsList.get(i).getName().equals(nameOfSponsor)) {
							File videoFile = new File(video);
							// Creates a new metadata object to get all the
							// informations
							// about
							// the
							// selected video
							// and verify if this video can be inserted in the
							// list
							Metadata metadata = new Metadata(videoFile);

							if (sponsorsList.get(i).getType().equals("Silver")) {
								if (sponsorsList.get(i).getCurrentDuration()
										+ metadata.getDuration() > 120) {
									throw new Exception(
											"Ultrapassou o limite de tempo de video!");
								} else {

									sponsorsList
											.get(i)
											.setCurrentDuration(
													(sponsorsList
															.get(i)
															.getCurrentDuration() + metadata
															.getDuration()));

									writer.writeVideo(sponsorsList.get(i)
											.getName(), video, 0, 0);

								}
							} else if (sponsorsList.get(i).getType().equals(
									"Gold")) {
								if (sponsorsList.get(i).getCurrentDuration()
										+ metadata.getDuration() > 180) {
									throw new Exception(
											"Ultrapassou o limite de tempo de video!");
								} else {
									sponsorsList.get(i).setCurrentDuration(
											sponsorsList.get(i)
													.getCurrentDuration()
													+ metadata.getDuration());

									writer.writeVideo(sponsorsList.get(i)
											.getName(), video, 0, 0);
								}
							} else if (sponsorsList.get(i).getType().equals(
									"Platinum")) {
								if (sponsorsList.get(i).getCurrentDuration()
										+ metadata.getDuration() > 240) {
									throw new Exception(
											"Ultrapassou o limite de tempo de video!");
								} else {
									sponsorsList.get(i).setCurrentDuration(
											sponsorsList.get(i)
													.getCurrentDuration()
													+ metadata.getDuration());
									writer.writeVideo(sponsorsList.get(i)
											.getName(), video, 0, 0);
								}
							}
						}
					}
				} else {
					JOptionPane.showMessageDialog(getRootPane(),
							"Este video já foi adicionado!", "Aviso!",
							JOptionPane.WARNING_MESSAGE);
				}

			} catch (Exception e) {
				JOptionPane.showMessageDialog(getRootPane(), e.getMessage(),
						"Erro!", JOptionPane.ERROR_MESSAGE);
			}
		} else {

		}

	}

	private void jMenuItemAddRssActionPerformed(java.awt.event.ActionEvent evt)
			throws Exception {
		// Add the URL of the RSS
		try {

			String url = JOptionPane.showInputDialog(getRootPane(),
					"Digite a URL do feed", "Adicionar RSS",
					JOptionPane.PLAIN_MESSAGE);

			News news = new News(url);
			newsMap.put(news.getFeedTitle(), news);
			newsList.add(news);

			JOptionPane.showMessageDialog(getRootPane(),
					"RSS adicionada com sucesso!", "Adicionar RSS",
					JOptionPane.PLAIN_MESSAGE);

		} catch (NullPointerException nullPointerException) {
			// Do nothing if a null entry was passed
		} catch (MalformedURLException malformedException) {
			JOptionPane
					.showMessageDialog(
							getRootPane(),
							"Houve um erro ao tentar adicionar a RSS: \n URL Invalida! \n",
							"Erro!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemRemoveRssActionPerformed(
			java.awt.event.ActionEvent evt) {
		// Removes the RSS from the system
		try {

			String[] options = new String[newsList.size()];

			for (int i = 0; i < newsList.size(); i++) {
				options[i] = newsList.get(i).getFeedTitle();
			}

			String url = (String) JOptionPane.showInputDialog(getRootPane(),
					"Selecione a RSS a ser removida", "Remove RSS",
					JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

			newsMap.remove(url);
			for (int j = 0; j < newsList.size(); j++) {
				if (newsList.get(j).getFeedTitle().equals(url)) {
					newsList.remove(newsList.get(j));
				}
			}

		} catch (Exception e) {
			JOptionPane.showMessageDialog(getRootPane(),
					"Não há RSS cadastradas no sistema", "Erro!",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemShowAvailableActionPerformed(
			java.awt.event.ActionEvent evt) {
		// Shows all URLs available on the system
		if (newsMap.isEmpty()) {
			JOptionPane.showMessageDialog(getRootPane(),
					"Não há RSS cadastradas no sistema", "Erro",
					JOptionPane.ERROR_MESSAGE);
		} else {
			String availableNews = "RSS Disponíveis \n";
			for (int i = 0; i < newsMap.size(); i++) {
				availableNews += (String) newsMap.keySet().toArray()[i] + '\n';
			}
			JOptionPane.showMessageDialog(getRootPane(), availableNews,
					"RSS Disponíveis", JOptionPane.PLAIN_MESSAGE);
		}
	}

	private void jMenuItemUpdateNewsActionPerformed(
			java.awt.event.ActionEvent evt) {
		// Refresh the selected RSS
		try {

			String[] options = new String[newsList.size()];

			for (int i = 0; i < newsList.size(); i++) {
				options[i] = newsList.get(i).getFeedTitle();
			}

			String urlUpdating = (String) JOptionPane.showInputDialog(
					getRootPane(), "Selecione a RSS a ser atualizada",
					"Remove RSS", JOptionPane.PLAIN_MESSAGE, null, options,
					options[0]);

			if (urlUpdating != null) {
				newsMap.get(urlUpdating).updateNews();
				JOptionPane.showMessageDialog(null,
						"Rss Atualizada com sucesso!");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(getRootPane(),
					"Não há RSS cadastradas no sistema", "Erro!",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemStartExibitionActionPerformed(
			java.awt.event.ActionEvent evt) throws Exception {

		videoOrganizer = new VideoOrganizer(reader);

		this.setVisible(true);

		Player p = null;
		try {
			News n = null;
			p = new Player(videoOrganizer.getOrganizedPlayList(), n);
			SwingUtilities.invokeLater(p.createPlayer());
			p.play();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(getRootPane(), e.getMessage(),
					"Erro!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {
		System.exit(0);
	}

	private void jMenuItemRemoveVideoActionPerformed(
			java.awt.event.ActionEvent evt) {

		try {
			List<String> names = reader.getSponsorsName();
			String[] sponsorsOptions = new String[names.size()];
			Iterator<String> it = names.iterator();
			while (it.hasNext()) {
				int count = 0;
				sponsorsOptions[count] = it.next();
				count++;
			}

			String chosenSponsor = (String) JOptionPane.showInputDialog(
					getRootPane(), "Selecione o patrocinador", "Remover video",
					JOptionPane.PLAIN_MESSAGE, null, sponsorsOptions,
					sponsorsOptions[0]);

			List<String> paths = reader.getPathOwnedBySponsor(chosenSponsor);
			String[] pathOptions = new String[paths.size()];
			Iterator<String> it2 = paths.iterator();
			while (it2.hasNext()) {
				int count = 0;
				pathOptions[count] = it2.next();
				count++;

			}

			String chosenPath = (String) JOptionPane.showInputDialog(
					getRootPane(), "Selecione o path", "Remover video",
					JOptionPane.PLAIN_MESSAGE, null, pathOptions,
					pathOptions[0]);

			updater.removeVideoFromDB(chosenPath);
			this.setVisible(false);

		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "ERROR!\n" + e.getMessage(),
					"Erro!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemVideoAvailableActionPerformed(
			java.awt.event.ActionEvent evt) {
	}

	private void jMenuItemAddSponsorActionPerformed(
			java.awt.event.ActionEvent evt) {
		// Saves the name, type and monthly and calls Sponsor constructor
		try {
			String name = JOptionPane.showInputDialog(this,
					"Digite o nome do Patrocinador", "Adicionar Patrocinador",
					JOptionPane.PLAIN_MESSAGE);

			for (int i = 0; i < sponsorsList.size(); i++) {
				if (sponsorsList.get(i).getName().toLowerCase().equals(
						name.toLowerCase())) {
					throw new Exception(
							"Patrocinador já cadastrado no sistema!");
				}
			}

			String[] options = { "Silver", "Gold", "Platinum" };
			String type = (String) JOptionPane.showInputDialog(getRootPane(),
					"Selecione o tipo de patrocinador",
					"Adicionar Patrocinador", JOptionPane.PLAIN_MESSAGE, null,
					options, options[0]);

			double monthly = Double.parseDouble(JOptionPane.showInputDialog(
					this, "Digite a mensalidade do Patrocinador",
					"Adicionar Patrocinador", JOptionPane.PLAIN_MESSAGE));

			Sponsor sponsor = new Sponsor(name, type, monthly);
			// Writes the sponsor in database
			writer.writeSponsor(name, monthly, type);
			// Add the sponsor in a list of sponsors
			sponsorsList.add(sponsor);
		} catch (NullPointerException nullPointerException) {
			// Do nothing if a null entry was passed
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Entrada(s) Inválida(s)!\n"
					+ e.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemSponsorAvailableActionPerformed(
			java.awt.event.ActionEvent evt) {
		// Reads all the sponsors stored in database
		try {
			sponsorsNameList = reader.getSponsorsName();
			sponsorsTypeList = reader.getSponsorType();
			sponsorsMonthlyList = reader.getSponsorMonthly();
		} catch (SQLException sqlException) {
			JOptionPane.showMessageDialog(getRootPane(), sqlException
					.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
		} catch (ClassNotFoundException classNotFoundException) {
			JOptionPane.showMessageDialog(getRootPane(), classNotFoundException
					.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
		}
		try {
			if (sponsorsNameList.size() == 0) {
				throw new Exception(
						"Não há Patrocinadores cadastrados no sistema!");
			}

			String availableSponsors = "     Patrocinadores     \n";
			for (int i = 0; i < sponsorsNameList.size(); i++) {
				availableSponsors += "Nome: " + sponsorsNameList.get(i)
						+ "               " + "Tipo: "
						+ sponsorsTypeList.get(i) + "               "
						+ "Mensalidade: " + sponsorsMonthlyList.get(i) + '\n';
			}
			JOptionPane.showMessageDialog(this, availableSponsors,
					"Patrocinadores Disponíveis", JOptionPane.PLAIN_MESSAGE);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(getRootPane(), e.getMessage(),
					"Aviso!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemRemoveSponsorActionPerformed(
			java.awt.event.ActionEvent evt) {
		try {

			if (sponsorsList.size() == 0) {
				throw new Exception(
						"Não há Patrocinadores cadastrados no sistema!");
			}

			Object[] options = new Object[sponsorsList.size()];
			for (int i = 0; i < sponsorsList.size(); i++) {
				options[i] = sponsorsList.get(i).getName();
			}
			String selectedSponsor = (String) JOptionPane.showInputDialog(this,
					"Selecione o patrocinador a ser removido",
					"Remover Patrocinador", JOptionPane.QUESTION_MESSAGE, null,
					options, options[0]);

			for (int i = 0; i < sponsorsList.size(); i++) {
				if (sponsorsList.get(i).getName().equals(selectedSponsor)) {
					updater.removeSponsorFromBD(sponsorsList.get(i).getName());
					sponsorsList.remove(i);
				}
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(getRootPane(), e.getMessage(),
					"Aviso!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void jMenuItemChangeLevelSponsorActionPerformed(
			java.awt.event.ActionEvent evt) {
		try {
			if (sponsorsList.size() == 0) {
				throw new Exception(
						"Não há Patrocinadores cadastrados no sistema!");
			}

			Object[] options = new Object[sponsorsList.size()];
			for (int i = 0; i < sponsorsList.size(); i++) {
				options[i] = sponsorsList.get(i).getName();
			}

			String selectedSponsor = (String) JOptionPane.showInputDialog(this,
					"Selecione o patrocinador a ser alterado",
					"Remover Patrocinador", JOptionPane.QUESTION_MESSAGE, null,
					options, options[0]);

			Object[] typeOptions = { "Silver", "Gold", "Platinum" };
			String type = (String) JOptionPane.showInputDialog(getRootPane(),
					"Selecione o tipo desejado",
					"Modificar Tipo de Patrocinador",
					JOptionPane.PLAIN_MESSAGE, null, typeOptions,
					typeOptions[0]);

			for (int i = 0; i < sponsorsList.size(); i++) {
				if (sponsorsList.get(i).getName().equals(selectedSponsor)) {
					sponsorsList.get(i).setType(type);
					updater.updateSponsorType(sponsorsList.get(i).getName(),
							type);
				}
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(getRootPane(), e.getMessage(),
					"Aviso!", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void JMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {
		String message = "MarketingJoy \n Sistema de apoio ao Entretenimento, Informação e Publicidade para Ambientes Comerciais \n \n Desenvolvido por: \n - Cassio Espindola \n - Cassio Cabral \n - Heitor Meira \n - Igor Ruda \n \n Orientado por: \n - Fabio Leal(Monitor) \n - Raquel Lopes(Professora) \n \n Este Software foi desenvolvido por alunos do curso de Ciencia da Computacao da Universidade \n Federal de Campina Grande(UFCG) para a disciplina de Laboratorio de Programacao II.";
		JOptionPane
				.showMessageDialog(getRootPane(), message,
						"Informações sobre o Software",
						JOptionPane.INFORMATION_MESSAGE);
	}

	/**
	 * @param args
	 *            - the command line arguments
	 */
	public static void main(String args[]) {
		ExtractVideo extractVideo = new ExtractVideo();
		extractVideo.extractVideo();

		Gst.init();
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				MainScreen mainScreen;
				mainScreen = new MainScreen();
				mainScreen.setVisible(true);
			}
		});
		Gst.main();
	}

	// Variables declaration - do not modify
	private javax.swing.JLabel JLabelLogotype;
	private javax.swing.ImageIcon ImageIconLogotype;
	private javax.swing.JMenuItem JMenuItemAbout;
	private javax.swing.JMenu JMenuNews;
	private javax.swing.JDialog dialogAddVideo;
	private javax.swing.JFileChooser jFileChooserAddVideo;
	private javax.swing.JMenuBar jMenuBar1;
	private javax.swing.JMenu jMenuHelp;
	private javax.swing.JMenuItem jMenuItemAddRss;
	private javax.swing.JMenuItem jMenuItemAddSponsor;
	private javax.swing.JMenuItem jMenuItemChangeLevelSponsor;
	private javax.swing.JMenuItem jMenuItemAddVideo;
	private javax.swing.JMenuItem jMenuItemExit;
	private javax.swing.JMenuItem jMenuItemRemoveRss;
	private javax.swing.JMenuItem jMenuItemRemoveSponsor;
	private javax.swing.JMenuItem jMenuItemRemoveVideo;
	private javax.swing.JMenuItem jMenuItemShowAvailable;
	private javax.swing.JMenuItem jMenuItemSponsorAvailable;
	private javax.swing.JMenuItem jMenuItemStartExibition;
	private javax.swing.JMenuItem jMenuItemUpdateNews;
	private javax.swing.JMenu jMenuManager;
	private javax.swing.JMenu jMenuStart;
	private javax.swing.JMenu jMenuVideo;

	// End of variables declaration

}
